<?php

namespace App\Http\Controllers\Admin;

use App\Http\Filters\OrderFilter;
use App\Models\Order;
use App\Http\Requests\OrderRequest;
use App\Http\Resources\OrderResource;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\OrderExport;

class OrderController extends Controller
{
    public function index(OrderFilter $filter)
    {
        $orders = Order::query()
            ->with('user','housings')
            ->filter($filter)
            ->paginate();

        return $this->ok(OrderResource::collection($orders));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(OrderRequest $request)
    {
        $inputs = $request->validated();
        $order = Order::create($inputs);

        return $this->created(OrderResource::make($order));
    }

    public function edit(Request $request, Order $order)
    {
        return $this->ok(OrderResource::make($order));
    }

    public function update(OrderRequest $request, Order $order)
    {
        $inputs = $request->validated();
        $order->update($inputs);

        return $this->created(OrderResource::make($order));
    }

    public function destroy(Order $order)
    {
        $order->delete();
        return $this->noContent();
    }
    public function export(Request $request)
    {
        $filters = $request->only([
            'ids',
            // ... 其他可能的过滤条件
        ]);
        return Excel::download(new OrderExport($filters), '订单.xlsx');
    }
}
